% SWI versions of builtins
% Required for bootstrapping

read_character(Char) :- get_single_char(Char).
read_line(String) :- current_input(CommandLine), read_line_to_codes(CommandLine,String).
write_character(Char) :- put_code(Char).
write_string(String) :- maplist(put_code,String).

get_file_contents(Filename, String) :-
  open(Filename, read, Fd),
  get_contents(Fd, String),
  close(Fd).
get_contents(Fd,[]) :- at_end_of_stream(Fd), !.
get_contents(Fd,[C|Cs]) :- get_code(Fd,C), get_contents(Fd,Cs).


open_file(Filename, Read_or_Write, Handle) :-
 atom_codes(FilenameA, Filename),
 open(FilenameA, Read_or_Write, Handle, []).
write_string(Handle,[]).
write_string(Handle,[C|Cs]) :- put_code(Handle,C), write_string(Handle,Cs).


:- multifile('$my$term_expansion'/2).
'$my$expand_term'(Term,Expansion) :- '$my$term_expansion'(Term,Expansion) -> true ; Term = Expansion.

smoke_test :-
 consult('library.pl'),
 consult('parse.pl'),
 write('boot.pl'),nl,parse_file('boot.pl',_),
 write('dcg.pl'),nl,parse_file('dcg.pl',_),
 write('parse.pl'),nl,parse_file('parse.pl',_),
 consult('dcg.pl'),
 consult('compile.pl').

boot :-
 compile_file("BootSink", 'boot.pl', _),
 compile_file("DCGSink", 'dcg.pl', _),
 compile_file("LibrarySink", 'library.pl', _),
 compile_file("ParseSink", 'parse.pl', _),
 compile_file("CompileSink", 'compile.pl', _),
 compile_file("MainSink", 'interact.pl', _),
 compile_file("TestSink", 'test.pl', _),
 !.
